/*
 * MessagingPanel.cpp
 *
 *  Created on: Jan 26, 2012
 *      Author: erudhes
 */
#include <wx/richtext/richtextctrl.h>
#include <wx/socket.h>
#include "MessagingPanel.h"
#include "MainFrame.h"
#include "NetworkEventData.h"
#include "SmartPointer.h"
#include "App.h"

// TODO: Why do we need to overwrite the SetClientSize method?

BEGIN_EVENT_TABLE(MessagingPanel, wxPanel)
EVT_TEXT_ENTER(MainFrame::MESSAGING_FRAME_MESSAGE_SENDING_TEXT_CTRL_ID, MessagingPanel::onMessageSending)
EVT_BUTTON(MainFrame::MESSAGING_FRAME_MESSAGE_SENDING_BUTTON_ID, MessagingPanel::onMessageSending)
END_EVENT_TABLE()

MessagingPanel::MessagingPanel(wxFrame* parent): wxPanel(parent), mMessagingTextCtrl(NULL), mMessageSendingTextCtrl(NULL), mMessageSendingButton(NULL)
{
	// Creating all the controllers for the messaging window
	this->wxPanel::SetClientSize(WIDTH, HEIGHT);
	mMessagingTextCtrl = new wxRichTextCtrl(this, MainFrame::MESSAGING_FRAME_MESSAGING_TEXT_CTRL_ID, wxT(""), wxPoint(0,0), wxSize(WIDTH, HEIGHT - 20), wxRE_READONLY|wxRE_MULTILINE|wxHSCROLL);
	mMessageSendingTextCtrl = new wxTextCtrl(this, MainFrame::MESSAGING_FRAME_MESSAGE_SENDING_TEXT_CTRL_ID, wxT(""), wxPoint(0, HEIGHT - 20), wxSize(WIDTH - 100, 20), wxTE_PROCESS_ENTER);
	mMessageSendingButton = new wxButton(this, MainFrame::MESSAGING_FRAME_MESSAGE_SENDING_BUTTON_ID, wxT("Send"), wxPoint(WIDTH - 100, HEIGHT - 20), wxSize(100, 20));
}

void MessagingPanel::SetClientSize(int width, int height)
{
	wxASSERT_MSG(height == HEIGHT, wxString::Format(wxT("Height of MessagingPanel must be %d pixels!"), HEIGHT));
	this->wxPanel::SetClientSize(width, height);
	mMessagingTextCtrl->SetSize(width, 100);
	mMessageSendingTextCtrl->SetSize(width - 100, HEIGHT - 100);
	mMessageSendingButton->SetPosition(wxPoint(width - 100, 100));
}

void MessagingPanel::SetClientSize(const wxSize& size)
{
	this->SetClientSize(size.x, size.y);
}

void MessagingPanel::SetClientSize(const wxRect& rect)
{
	this->SetClientSize(rect.width, rect.height);
}

void MessagingPanel::onMessageSending(wxCommandEvent& event)
{
	SmartPointer<NetworkEventData> data = NULL;
	wxString message(wxT("[Your message]: ") + mMessageSendingTextCtrl->GetValue());
	wxSocketBase* socket = wxGetApp().getClientSocket();

	wxASSERT_MSG(socket != NULL, wxT("Client socket is not set in the application!!!"));
	// Only sending valid messages
	if(wxT("") == mMessageSendingTextCtrl->GetValue())
		return;
	switch(static_cast<wxWindow*>(event.GetEventObject())->GetId())
	{
		case MainFrame::MESSAGING_FRAME_MESSAGE_SENDING_TEXT_CTRL_ID:
			// fall through
		case MainFrame::MESSAGING_FRAME_MESSAGE_SENDING_BUTTON_ID:
			// The user's messages are shown in blue
			this->addMessage(message, WxUtils::BLUE);
			// Sending the data to the remote side
			data = new NetworkEventData();
			data->setDataType(NetworkEventData::MESSAGING);
			data->setStringData(mMessageSendingTextCtrl->GetValue());
			data->write(socket);
			mMessageSendingTextCtrl->SetValue(wxT(""));
			break;
		default:
			wxASSERT_MSG(false, wxT("MessagingPanel::onMessageSending called with wrong event object"));
			break;
	}
}

void MessagingPanel::addMessage(const wxString& message, WxUtils::Color color)
{
	mMessagingTextCtrl->BeginTextColour(wxColour(color.getRed(), color.getGreen(), color.getBlue()));
	mMessagingTextCtrl->WriteText(message);
	mMessagingTextCtrl->EndTextColour();
	mMessagingTextCtrl->Newline();
}
